package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrderDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : [wangtao]
 * @className : OrderController
 * @description : []
 * @createTime : [2022/10/13 13:04]
 * @updateTime : [2022/10/13 13:04]
 */
@RestController
@RequestMapping("/order")
@Slf4j

public class OrderController {
    @Autowired
 private   OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;

    @GetMapping("/userPage")
    public R<Page<Orders>> page(int page, int pageSize) {
        log.info("page{},pageSize={}", page, pageSize);
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.in(Orders::getUserId, userId);
        Page<Orders> pageInfo = new Page(page, pageSize);
orderService.page(pageInfo,lqw);
        List<Orders> records = pageInfo.getRecords();
        List<Long> collect = records.stream().map(item -> item.getId()).collect(Collectors.toList());
        LambdaQueryWrapper<OrderDetail> lqw1 = new LambdaQueryWrapper<>();


        if(collect.size()!=0){
            lqw1.in(OrderDetail::getOrderId, collect);
        }

        List<OrderDetail> orderDetails = orderDetailService.list(lqw1);
//        List<Orders> record = orderDetails.stream().map(item -> {
//            for (Orders order : records) {
//                if (order.getId().equals(item.getOrderId())) {
//                    order.setOrderDetails(item);
//                    order.setSumNum(order.getAmount());
//                    return order;
//                }
//            }
//            return null;
//        }).collect(Collectors.toList());

//        orderDetails.stream().filter()
        List<Orders> record = records.stream().map(item -> {
            Long orderId = item.getId();
            List<OrderDetail> orderDetaillist = orderDetails.stream().filter(index -> index.getOrderId().equals(orderId)).collect(Collectors.toList());
            item.setOrderDetails(orderDetaillist);
            item.setSumNum(item.getAmount());
            return item;
        }).collect(Collectors.toList());


        pageInfo.setRecords(record);
        return R.success(pageInfo);
    }
@PostMapping("/submit")
    public R sublimt(@RequestBody  Orders order){
    log.info("Order:{}",order);
  orderService.submit(order);
   return R.success("保存成功");
}

}
